package com.youlin;

import com.rabbitmq.client.Channel;
import com.youlin.entities.BygAdmin;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalTime;
import java.util.concurrent.TimeUnit;

//@Component
//@RabbitListener(queues = {"QUEUE_A", "QUEUE_B"}, containerFactory = "singleListenerContainer"/*, priority = "5"*/)
//@RabbitListener(queues = {"QUEUE_A", "QUEUE_B"}, containerFactory = "multiListenerContainer")
public class RabbitMqListener {

//    @Transactional(transactionManager = "rabbitTransactionManager")
    @RabbitHandler(isDefault = true)
    public void RabbitHandlerTest1 (Channel channel, Message message) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.printf("\033[33m %s ===>> RabbitHandlerTest1 ===>> %s ===>> %s \033[0m\n",
                LocalTime.now(), Thread.currentThread().getName(), new String(message.getBody()));
        TimeUnit.MILLISECONDS.sleep(123);
//        channel.basicAck(deliveryTag, false);
//        int i = 10/0;
    }

    @RabbitHandler
    public void RabbitHandlerTest2 (Channel channel, Message msg, org.springframework.messaging.Message<String> message) throws Exception {
        System.out.printf("\033[32m RabbitHandlerTest2-false ===>> %s \033[0m\n", message.getPayload());
        long deliveryTag = msg.getMessageProperties().getDeliveryTag();
        channel.basicAck(deliveryTag, false);
    }

    @RabbitHandler
    public void RabbitHandlerTest3 (Channel channel, Message msg, BygAdmin admin) throws Exception {
        channel.basicQos(1);
        long threadId = Thread.currentThread().getId();
        System.out.printf("\033[31m RabbitHandlerTest3-false ===>> %s ===>> %s \033[0m\n", threadId, admin);
        long deliveryTag = msg.getMessageProperties().getDeliveryTag();
        channel.basicAck(deliveryTag, false);
//        if (threadId == 74) channel.basicAck(deliveryTag, true);
    }

}
